package com.zhl.media.app.mvp.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.text.TextPaint;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.zhl.media.app.R;
import com.zhl.media.app.model.Node;
import com.zhl.media.app.model.Proj;

import java.util.List;

public class WorkTaskTreeAdapter extends TreeListViewAdapter<Proj> {
    public WorkTaskTreeAdapter(ListView mTree, Context context, List<Proj> datas, int defaultExpandLevel) throws IllegalArgumentException, IllegalAccessException {
        super(mTree, context, datas, defaultExpandLevel);
    }
    @Override
    public View getConvertView(Node node, int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.work_task_list_item, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.icon = (ImageView) convertView.findViewById(R.id.id_treenode_icon);
            viewHolder.label = (TextView) convertView.findViewById(R.id.id_treenode_label);
            viewHolder.num = (TextView) convertView.findViewById(R.id.num);
            viewHolder.indexIcon = (ImageView) convertView.findViewById(R.id.index_icon);
            viewHolder.over = (ImageView) convertView.findViewById(R.id.over);
            viewHolder.tags = (ImageView) convertView.findViewById(R.id.tags);
            viewHolder.tagsgreen = (ImageView) convertView.findViewById(R.id.tagsgreen);
            viewHolder.desLayout = (LinearLayout)convertView.findViewById(R.id.des);
            viewHolder.sztz = (TextView) convertView.findViewById(R.id.sztz);
            viewHolder.szbh = (TextView) convertView.findViewById(R.id.szbh);
            viewHolder.ytz = (TextView) convertView.findViewById(R.id.ytz);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        if(node.isRoot()){
            TextPaint tp = viewHolder.label.getPaint();
            tp.setFakeBoldText(true);
            viewHolder.label.setTextColor(ContextCompat.getColor(mContext,R.color.public_colorAccent));
            convertView.setBackgroundColor(Color.parseColor("#f0f0f0"));
        }else {
            TextPaint tp = viewHolder.label.getPaint();
            tp.setFakeBoldText(false);
            convertView.setBackgroundColor(Color.parseColor("#00000000"));
            viewHolder.label.setTextColor(ContextCompat.getColor(mContext,R.color.public_color_333333));
        }

        if (node.getIcon() == -1 || node.isRoot()) {
            viewHolder.icon.setVisibility(View.INVISIBLE);
        } else {
            viewHolder.icon.setVisibility(View.VISIBLE);
            viewHolder.icon.setImageResource(node.getIcon());
        }

        if (node.getLevel() == 1){
            viewHolder.desLayout.setVisibility(View.VISIBLE);
            viewHolder.num.setVisibility(View.VISIBLE);
            if(node.getType() == 2){
                viewHolder.over.setVisibility(View.GONE);
                viewHolder.num.setText("空余["+node.getKymtwNum()+"]");
                viewHolder.num.setTextColor(ContextCompat.getColor(mContext,R.color.public_color_FF06CB7E));
            }else {
                if(node.getYiNum() == node.getSumNum()){
                    viewHolder.num.setText(node.getYiNum() + "/" + node.getSumNum());
                    viewHolder.num.setTextColor(ContextCompat.getColor(mContext,R.color.public_color_FF4A4A4A));
                    viewHolder.over.setVisibility(View.VISIBLE);
                }else {
                    viewHolder.num.setText(node.getYiNum() + "/" + node.getSumNum());
                    viewHolder.num.setTextColor(ContextCompat.getColor(mContext,R.color.public_color_d20004));
                    viewHolder.over.setVisibility(View.GONE);
                }
            }
            if(node.getSsbhNum()==0 && node.getSttzNum()==0 && node.getYtzNum() ==0){
                viewHolder.desLayout.setVisibility(View.GONE);
            }else {
                viewHolder.desLayout.setVisibility(View.VISIBLE);
                if(node.getSsbhNum()==0){
                    viewHolder.szbh.setVisibility(View.GONE);
                }else {
                    viewHolder.szbh.setVisibility(View.VISIBLE);
                    viewHolder.szbh.setText("设置补画[" + node.getSsbhNum() + "]");
                }
                if(node.getSttzNum()==0){
                    viewHolder.sztz.setVisibility(View.GONE);
                }else {
                    viewHolder.sztz.setVisibility(View.VISIBLE);

                    viewHolder.sztz.setText("设置调整[" + node.getSttzNum() + "]");
                }
                if(node.getYtzNum()==0){
                    viewHolder.ytz.setVisibility(View.GONE);
                }else {
                    viewHolder.ytz.setVisibility(View.VISIBLE);

                    viewHolder.ytz.setText("已调整[" + node.getYtzNum() + "]");
                }

            }

        } else {
            viewHolder.desLayout.setVisibility(View.GONE);
            viewHolder.num.setVisibility(View.GONE);
            viewHolder.over.setVisibility(View.GONE);
        }
        // 控制ICON
        if (node.getLevel() == 2){
            viewHolder.indexIcon.setVisibility(View.VISIBLE);
            viewHolder.indexIcon.setImageDrawable(ContextCompat.getDrawable(mContext,R.drawable.louzuo));
        }else {
            viewHolder.indexIcon.setVisibility(View.GONE);
        }
        // 控制小圆点
        if(node.getLevel() == 2 || node.getLevel() == 3){
            if(node.getSumNum()==0){
                viewHolder.tags.setVisibility(View.GONE);
                viewHolder.tagsgreen.setVisibility(View.GONE);
            }else {
                if(node.getSumNum() != node.getYiNum()){
                    viewHolder.tagsgreen.setVisibility(View.GONE);
                    viewHolder.tags.setVisibility(View.VISIBLE);
                } else {
                    viewHolder.tagsgreen.setVisibility(View.VISIBLE);
                    viewHolder.tags.setVisibility(View.GONE);
                }
            }
        }else {
            viewHolder.tags.setVisibility(View.GONE);
            viewHolder.tagsgreen.setVisibility(View.GONE);
        }
        viewHolder.label.setText(node.getName());

        return convertView;
    }

    private final class ViewHolder {
        ImageView icon;
        TextView label;
        TextView num;
        ImageView indexIcon;
        ImageView over;
        ImageView tags;
        ImageView tagsgreen;
        LinearLayout desLayout;
        TextView sztz;
        TextView szbh;
        TextView ytz;
    }
}
